package walletadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateCurrencyExchangeConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateCurrencyExchangeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCurrencyExchangeConfigLogic {
	return &UpdateCurrencyExchangeConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改钱包币种兑换配置
func (l *UpdateCurrencyExchangeConfigLogic) UpdateCurrencyExchangeConfig(in *pb.UpdateCurrencyExchangeConfigReq) (*pb.UpdateCurrencyExchangeConfigResp, error) {
	defer utils.RecoverPanic()

	var ucec *wallet.CurrencyExchangeConfig
	var err error
	ucec, err = l.svcCtx.CurrencyExchangeConfigModel.FindOne(l.ctx, in.Id)
	if err != nil && err != wallet.ErrNotFound {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	if ucec == nil {
		l.Logger.Errorf("CurrencyExchangeConfig not found id %d ", in.Id)
		return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "not found")
	}

	//if in.MoneyAmount != "" {
	//	var dMoneyAmount decimal.Decimal
	//	dMoneyAmount, err = decimal.NewFromString(in.MoneyAmount)
	//	if err != nil {
	//		return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "money amount error")
	//	}
	//	ucec.MoneyAmount, _ = dMoneyAmount.RoundFloor(2).Float64()
	//}
	//
	//if in.CurrencyAmount != "" {
	//	var dCurrencyAmount decimal.Decimal
	//	dCurrencyAmount, err = decimal.NewFromString(in.MoneyAmount)
	//	if err != nil {
	//		return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "currency amount error")
	//	}
	//	ucec.CurrencyAmount, _ = dCurrencyAmount.RoundFloor(2).Float64()
	//}
	if in.MoneyName != "" {
		ucec.MoneyName = in.MoneyName
	}
	if in.CurrencyName != "" {
		ucec.CurrencyName = in.CurrencyName
	}
	if in.MoneyIcon != "" {
		ucec.MoneyIcon = in.MoneyIcon
	}
	if in.CurrencyIcon != "" {
		ucec.CurrencyIcon = in.CurrencyIcon
	}
	if in.State != 0 {
		ucec.State = in.State
	}

	err = l.svcCtx.CurrencyExchangeConfigModel.Update(l.ctx, ucec)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	return &pb.UpdateCurrencyExchangeConfigResp{}, nil
}
